/**
 * 
 */
package org.swing.utility.input.component;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.swing.utility.input.util.GUIUtil;

/**
 * @author lqnhu
 *
 */
public class FormPane extends JPanel {
	/**
	 * Create a FormPane
	 */
	public FormPane() {
		setLayout(new BorderLayout());
		inner = new JPanel(new GridBagLayout());
		add(inner, BorderLayout.NORTH);
		setBorder(BorderFactory.createEmptyBorder());
		inner.setBorder(BorderFactory.createEmptyBorder());
	}

	/**
	 * Add a field to this panel
	 * 
	 * @param prompt
	 *            The name for the field
	 * @param comp
	 *            The component to add alongside the label
	 */
	public void addEntry(String prompt, String tooltip, Component comp) {
		JLabel label = new JLabel(prompt);
		label.setToolTipText(tooltip);

		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.LINE_END;
		c.insets = new Insets(5, 5, 5, 5);

		c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
		c.fill = GridBagConstraints.NONE; // reset to default
		c.weightx = 0.0; // reset to default
		inner.add(label, c);

		c.gridwidth = GridBagConstraints.REMAINDER; // end row
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		inner.add(comp, c);

		comps.put(prompt + SUFFIX_LABEL, label);
		comps.put(prompt + SUFFIX_COMP, comp);
	}

	/**
	 * Add a field to this panel
	 * 
	 * @param prompt
	 *            The name for the field
	 */
	public void removeEntry(String prompt) {
		inner.remove(comps.get(prompt + SUFFIX_LABEL));
		inner.remove(comps.get(prompt + SUFFIX_COMP));

		comps.remove(prompt + SUFFIX_LABEL);
		comps.remove(prompt + SUFFIX_COMP);
	}

	/**
	 * Is this panel empty
	 */
	public boolean isEmpty() {
		return comps.size() == 0;
	}

	/**
	 * Get a list of the labels
	 */
	public String[] getFieldNames() {
		int count = getComponentCount() / 2;
		String[] list = new String[count];

		for (int i = 0; i < count; i++) {
			JLabel label = (JLabel) getComponent(i * 2);
			list[i] = label.getText();
		}

		return list;
	}

	/**
	 * Get at list of the values in the fields
	 */
	public String[] getFieldValues() {
		int count = getComponentCount() / 2;
		String[] list = new String[count];

		for (int i = 0; i < count; i++) {
			Component comp = getComponent(i * 2 + 1);
			list[i] = GUIUtil.getText(comp);
		}

		return list;
	}

	private static final String SUFFIX_COMP = "_comp";
	private static final String SUFFIX_LABEL = "_label";

	private JPanel inner;

	/**
	 * A store of the available components
	 */
	protected Hashtable<String, Component> comps = new Hashtable<String, Component>();

	/**
	 * Serialization ID
	 */
	private static final long serialVersionUID = 3258135738867790641L;
}
